/**
 * 
 */
package teehee.UI;

import java.util.ArrayList;

import teehee.Form.AddNewForm;
import teehee.Form.SearchPageForm;
import teehee.homepage.R;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

/**
 * @author 
 *
 */
public class AddNewExercise extends Activity
{
	
    boolean nameNullFlag;
    boolean nameInvalidFlag;
    
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_new_exercise);
        
        Button submit = ( Button ) findViewById(R.id.submitExerciseButton);
        submit.setOnClickListener( new ButtonClickHandler()  );
    }
    
    public class ButtonClickHandler implements View.OnClickListener {
		public void onClick( View view ) {
	        EditText inputName = (EditText) findViewById(R.id.enterExerciseName);
	        EditText inputDescription = (EditText) findViewById(R.id.enterExerciseDescription);
	        
	        String name = inputName.getText().toString().trim();
	        String description = inputDescription.getText().toString().trim();
	        ArrayList<String> exercises = new ArrayList<String>();
	        exercises.add("0");
	        
			nameNullFlag = false;
			nameInvalidFlag = false;
	        
			int nameValidation = AddNewForm.validateName(name);
			if (nameValidation == -1) {
				nameNullFlag = true;
			} else if(nameValidation == 0){
				nameInvalidFlag = true;
			}
			
			if(nameNullFlag){
				toaster("Name cannot be empty");
				return;
			}
			if(nameInvalidFlag){
				toaster("Name cannot contain special characters or consecutive spaces");
				return;
			}
			
			if (nameValidation == 1) {
				boolean result = AddNewForm.createNewEntryForm(getApplicationContext(), "ex", name, "0", "0", "0", description, exercises);
				if (result == false) {
					toaster("Name has been already used");
					return;
				} else {
					toaster("Exercise has been successfully added");
				}
			}
			
			AddNewTabs.entry = SearchPageForm.processInput(name);
			AddNewTabs.goHistoryPage = true;
			Intent history = new Intent(AddNewExercise.this, ExerciseHistory.class);
    		startActivity(history);	        
		}
	}
    
	public void toaster(String errormessage)
	{

		Context context = getApplicationContext();
		Toast toast = Toast.makeText(context, errormessage, Toast.LENGTH_LONG);
		toast.setGravity(Gravity.TOP|Gravity.LEFT, 25, 300);
		toast.show();
		
	}
	
    //Eric: this will allow the user to go back to the homepage from the add exercise tab
    @Override 
    public boolean onKeyDown(int keyCode, KeyEvent event) 
    {
        if ((keyCode == KeyEvent.KEYCODE_MENU))
        { 
        	//setContentView(R.layout.main);
        	Intent homep = new Intent(AddNewExercise.this, MainPage.class);
    		startActivity(homep);
            return true;
        }
        
        if ((keyCode == KeyEvent.KEYCODE_SEARCH))
        { 
        	//setContentView(R.layout.main);
        	Intent homep = new Intent(AddNewExercise.this, SearchPage.class);
    		startActivity(homep);
            return true;
        }
        
        return super.onKeyDown(keyCode, event);    
    } 
    
}